﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TinyCinema.Data;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;


namespace TinyCinema.Bussiness.Schedule
{
    public class SchedulePrice:DependencyObject
    {
        public static DependencyProperty lstSchedulePriceProperty = DependencyProperty.Register("lstSchedulePrice", typeof(ObservableCollection<Data.SchedulePrice>), typeof(SchedulePrice));
        public ObservableCollection<Data.SchedulePrice> lstSchedulePrice
        {
            get { return (ObservableCollection<Data.SchedulePrice>)GetValue(lstSchedulePriceProperty); }
            set
            {
                SetValue(lstSchedulePriceProperty, value);
            }
        }
        public static DependencyProperty SelectedIndexProperty = DependencyProperty.Register("SelectedIndex", typeof(int), typeof(SchedulePrice));
        public int SelectedIndex
        {
            get { return (int)GetValue(SelectedIndexProperty); }
            set
            {
                SetValue(SelectedIndexProperty, value);
            }
        }
        public ICommand CreateCommand { get; set; }
        public ICommand DeleteCommand { get; set; }
        public ICommand SaveCommand { get; set; }

        public Messenger Messenger { get; set; }

        public SchedulePrice()
        {
            Messenger = new Bussiness.Messenger();
            lstSchedulePrice = Data.SchedulePrice.GetAllSchedulePrice();
            CreateCommand = new RelayCommand<object>(
                (s) => true,
                (s) =>
                {
                    var Temp = new Data.SchedulePrice();
                    lstSchedulePrice.Add(Temp);
                    Temp.Save();
                });
            DeleteCommand = new RelayCommand<object>(
                (s) => true,
                (s) =>
                {
                    var message1 = new Message.DeleteConfirmation();
                    Messenger.Send(message1);
                    if (message1.Result == MessageBoxResult.Yes)
                    {
                        try
                        {
                            lstSchedulePrice[SelectedIndex].Delete();
                            lstSchedulePrice.RemoveAt(SelectedIndex);
                        }
                        catch
                        {
                            var message2 = new Message.DeleteFailed();
                            Messenger.Send(message2);
                            Data.Execution.Restore();
                        }

                    }
                });
            SaveCommand = new RelayCommand<Data.SchedulePrice>(
                (s) => true,
                (s) =>
                {
                    if (s != null) try { s.Save(); }
                        catch { Data.Execution.Restore(); }
                });

        }



    }
}
